package com.choosefine.it.company.controller;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.company.model.CompanyPublishproject;
import com.choosefine.it.company.model.vo.CompanyPublishManagerVo;
import com.choosefine.it.company.service.CompanyPublishManageService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 建筑公司的发布管理
 * @author chenjian
 * @date 2016-11-30 下午5:04:31
 */
@RestController
@RequestMapping("/companyPublishManage") 
@Api(value="建筑公司-我的发布管理",description="陈健")
public class CompanyPublishManageController extends BaseController{
	@Autowired
	private CompanyPublishManageService companyPublishManageService;
	Logger logger=LoggerFactory.getLogger(CompanyPublishManageController.class);
	
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userCode", value = "用户编号", required = true, dataType = "String", paramType = "path"),
		@ApiImplicitParam(name = "pageNum", value = "起始页", required = true, dataType = "int", paramType = "path"),
		@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "int", paramType = "path"),
		@ApiImplicitParam(name = "verifyStatus", value = "审核状态", required = true, dataType = "int", paramType = "path")})
	@RequestMapping(value="/getProjectList/{userCode}/{verifyStatus}/{pageNum}/{pageSize}",method=RequestMethod.GET)
	@ApiOperation(value="建筑公司查询发布的项目",response=CompanyPublishManagerVo.class)
	public String findPublishProjectList(@PathVariable String userCode, @PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable Integer verifyStatus){
		String result=null;
		PageDTO page= new PageDTO();
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		Map<String, Object> params = new HashMap<>();
		params.put("userCode", userCode);
		params.put("verifyStatus", verifyStatus);
		Page<CompanyPublishManagerVo> list = companyPublishManageService.findPublishProjectList(page, params);
		if(list.size()>0){
			result= responseSuccess(list);
		}else{
			result= responseFail( ResultCode.FAILED,ResultCode.MSG_DATA_ISEMPTY);	
		}
		return result;
	}
	@ApiOperation(value = "建筑公司查询项目的详情",response=CompanyPublishproject.class)
    @ApiImplicitParam(name = "publishId", value = "发布的ID", required = true, dataType = "int", paramType = "path")
	@RequestMapping(value="/getProjectInfoDetail/{publishId}",method=RequestMethod.GET)
	public String getProjectInfoDetail(@PathVariable Integer publishId) throws JsonProcessingException{
		String result=null;
		CompanyPublishproject companyPublishproject = companyPublishManageService.findCompanyPublishProjectDetailById(publishId);
		if(companyPublishproject!=null){
			result = responseSuccess(companyPublishproject, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
		}else {
			result = responseFail(ResultCode.FAILED, ResultCode.MSG_DATA_ISEMPTY);
		}
		return result;
		
	}
	
	@ApiOperation(value = "建筑公司修改项目发布状态")
	@ApiImplicitParams({@ApiImplicitParam(name = "publishId", value = "发布的ID", required = true, dataType = "int", paramType = "path"),
						@ApiImplicitParam(name = "publishStatus", value = "发布状态", required = true, dataType = "int", paramType = "path")})
	@RequestMapping(value="/updateCompanyPublishproject/{publishId}/{publishStatus}",method=RequestMethod.PUT)
	public String updateCompanyPublishproject(@PathVariable Integer publishId,
												  @PathVariable Integer publishStatus) throws JsonProcessingException{
		String result=null;
		if(null != publishId && null != publishStatus){
			Map<String, Object> params = new HashMap<>();
			params.put("id", publishId);
			params.put("publishStatus", publishStatus);
			Integer isSuccess = companyPublishManageService.updateCompanyPublishproject(params);
			if(isSuccess.intValue()>0){
				result = responseSuccess(isSuccess,ResultCode.SUCCESS,ResultCode.MSG_SUCCESS);
			}else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_UPDATE_FAILD);
			}
		}else {
			result = responseFail(ResultCode.FAILED, ResultCode.MSG_PARAMS_ERROR);
		}
		return result;
	}
	
	@ApiOperation(value = "建筑公司删除发布的项目信息")
	@ApiImplicitParam(name = "publishId", value = "发布的ID", required = true, dataType = "int", paramType = "query")
	@RequestMapping(value="/deleteCompanyPublishProject",method=RequestMethod.PUT)
	public String deleteCompanyPublishProject(@RequestParam Integer publishId) throws JsonProcessingException{
		String result=null;
		if(publishId!=null&&publishId.intValue()>0){
			Map<String, Object> params = new HashMap<>();
			params.put("id", publishId);
			params.put("utime",DateUtils.getNowTime());
			params.put("isValid",CommonConstant.INVALID);//1为删除发布信息
			int isSuccess = companyPublishManageService.deleteCompanyPublishProject(params);
			if(isSuccess>0){
				result = responseSuccess(isSuccess,ResultCode.SUCCESS,ResultCode.MSG_SUCCESS);
			}else {
				result = responseFail(ResultCode.FAILED, ResultCode.MSG_UPDATE_FAILD);
			}
		}else {
			result = responseFail(ResultCode.FAILED, ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	
	
	
}
